package org.mnj07.jms.queue;

import javax.annotation.Resource;
import javax.jms.*;

public class ReceiverDI
{
  @Resource(lookup = "jms/javaee6/ConnectionFactory")
  private static ConnectionFactory connectionFactory;
  @Resource(lookup = "jms/javaee6/Queue")
  private static Queue queue;
  
  public static void main(String[] args) {
    try {
        // Creates the needed artifacts to connect to the queue
        Connection connection = connectionFactory.createConnection();
        Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        MessageConsumer consumer = session.createConsumer(queue);
        connection.start();

        // Loops to receive the messages
        System.out.println("\nInfinite loop. Waiting for a message...");
        while (true) {
            TextMessage message = (TextMessage) consumer.receive();
            System.out.println("Message received: " + message.getText());
        }

    } catch (Exception e) {
        e.printStackTrace();
    }
  }
}
